Imports System.Data.SqlClient

Public Class AuthorData
    Implements IAuthorData

    Private Shared ConnectionString As String = Configuration.ConfigurationManager.ConnectionStrings("names").ConnectionString

    Private Function LoadAuthorFromDs(ByVal dba As Author) As TaxonNamesInterface.Author
        Dim a As New TaxonNamesInterface.Author

        a.Abbreviation = dba.Abbreviation
        a.AuthorId = dba.AuthorId
        a.ChangeDate = dba.ChangeDate
        a.ChangeUser = dba.ChangeUser
        a.CorrectAuthorId = dba.CorrectAuthorId
        a.Dates = dba.Dates
        a.Forename = dba.Forename
        a.IPNIAlternativeNames = dba.IPNIAlternativeNames
        a.IPNIAuthor_id = dba.IPNIAuthor_id
        a.IPNIVersion = dba.IPNIversion
        a.Surname = dba.Surname
        a.TaxonGroups = dba.TaxonGroups

        Return a
    End Function

    Public Function ListAuthors(ByVal searchText As String, ByVal anywhereInText As Boolean) As List(Of TaxonNamesInterface.Author) Implements IAuthorData.ListAuthors
        Dim auths As New List(Of TaxonNamesInterface.Author)
        Dim db As New NamesDB(ConnectionString)

        Dim ds
        If anywhereInText Then
            ds = From tbl In db.Authors Select tbl Where tbl.Abbreviation.Contains(searchText)
        Else
            ds = From tbl In db.Authors Select tbl Where tbl.Abbreviation.StartsWith(searchText)
        End If

        For Each a As Author In ds
            auths.Add(LoadAuthorFromDs(a))
        Next

        Return auths
    End Function

    Public Function GetAuthor(ByVal authorId As Integer) As TaxonNamesInterface.Author Implements IAuthorData.GetAuthor
        Dim auth As TaxonNamesInterface.Author = Nothing
        Dim db As New NamesDB(ConnectionString)

        Dim ds = From tbl In db.Authors Select tbl Where tbl.AuthorId = authorId
        auth = LoadAuthorFromDs(ds.First)

        Return auth
    End Function

    Public Function GetAuthorByName(ByVal author As String) As TaxonNamesInterface.Author Implements IAuthorData.GetAuthorByName
        Dim auth As TaxonNamesInterface.Author = Nothing
        Dim db As New NamesDB(ConnectionString)

        Dim ds = From tbl In db.Authors Select tbl Where tbl.Abbreviation = author
        auth = LoadAuthorFromDs(ds.First)

        Return auth
    End Function

    Public Sub SaveAuthor(ByVal auth As TaxonNamesInterface.Author, ByVal user As String) Implements IAuthorData.SaveAuthor
        Dim db As New NamesDB(ConnectionString)

        Dim dba As New Author
        If auth.AuthorId > 0 Then
            dba = (From tbl In db.Authors Select tbl Where tbl.AuthorId = auth.AuthorId).First
        End If

        dba.Abbreviation = auth.Abbreviation
        dba.ChangeDate = DateTime.Now
        dba.ChangeUser = user
        dba.CorrectAuthorId = auth.CorrectAuthorId
        dba.Dates = auth.Dates
        dba.Forename = auth.Forename
        dba.IPNIAlternativeNames = auth.IPNIAlternativeNames
        dba.IPNIAuthor_id = auth.IPNIAuthor_id
        dba.IPNIversion = auth.IPNIVersion
        dba.Surname = auth.Surname
        dba.TaxonGroups = auth.TaxonGroups

        If auth.AuthorId <= 0 Then
            db.Authors.InsertOnSubmit(dba)
        End If

        db.SubmitChanges()

        auth.AuthorId = dba.AuthorId
        auth.ChangeDate = dba.ChangeDate
        auth.ChangeUser = dba.ChangeUser
    End Sub


End Class
